import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:grocery_app_demo/core/utils/logger.dart';
import 'package:grocery_app_demo/pages/purchase_main/purchase_create/goods_info.dart';
import 'package:grocery_app_demo/widgets/gap.dart';

import 'logic.dart';

class CreatePurchasePage extends GetView<CreatePurchaseLogic> {
  const CreatePurchasePage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(CreatePurchaseLogic());

    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => Get.back(),
        ),
        title: Text('采购单'.tr),
        actions: [
          IconButton(
            icon: const Icon(Icons.check),
            onPressed: controller.submit,
          )
        ],
      ),
      body: Container(
        margin: gapAll16,
        child: Column(
          children: [
            Expanded(
              child: _buildListView(),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        tooltip: '添加商品'.tr,
        child: const Icon(Icons.add),
        onPressed: () => controller.addGoods(),
      ),
    );
  }

  Widget _buildHeaderCard() {
    return Card(
      child: Container(
        padding: gapAll16,
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                SizedBox(width: 100.w, child: Text('供应商'.tr)),
                Expanded(
                  child: Obx(() {
                    return DropdownButtonFormField<int?>(
                      items: controller.buildSupplier(),
                      value: controller.supplierId.value,
                      onChanged: (v) => controller.supplierId.value = v,
                      decoration: InputDecoration(
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(50),
                        ),
                      ),
                    );
                  }),
                ),
              ],
            ),
            gapH16,
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                SizedBox(width: 100.w, child: Text('收货仓'.tr)),
                Expanded(
                  child: Obx(() {
                    return DropdownButtonFormField<int?>(
                      items: controller.buildWarehouse(),
                      value: controller.warehouseId.value,
                      onChanged: (v) => controller.warehouseId.value = v,
                      decoration: InputDecoration(
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(50),
                        ),
                      ),
                    );
                  }),
                ),
              ],
            ),
            gapH16,
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                SizedBox(width: 100.w, child: Text('货主'.tr)),
                Expanded(
                  child: Obx(() {
                    return DropdownButtonFormField<int?>(
                      items: controller.buildShipper(),
                      onChanged: (v) => controller.shipperId.value = v,
                      value: controller.shipperId.value,
                      decoration: InputDecoration(
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(50),
                        ),
                      ),
                    );
                  }),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildListView() {
    return Obx(() {
      List<Widget> children = [];
      children.add(_buildHeaderCard());
      for (var g in controller.goodsMap.values) {
        children.add(buildCard(g));
      }
      return ListView(
        children: children,
      );
    });
  }

  Card buildCard(GoodsInfo g) {
    return Card(
      child: Column(
        children: [
          InkWell(
            child: Container(
              padding: gapAll16,
              child: Column(
                children: [
                  Row(
                    children: [
                      Text(
                        g.goodsName,
                        style: TextStyle(
                          fontSize: 30.w,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ],
                  ),
                  gapH16,
                  Row(
                    children: [
                      const Text(
                        '采购数量：',
                        textAlign: TextAlign.end,
                      ),
                      Text('${g.totalCount} ${g.goodsUnit.label}')
                    ],
                  ),
                  g.expand
                      ? Column(
                          children: [
                            Row(
                              children: [
                                const Text('采购单价：'),
                                Text('¥ ${g.price} / ${g.goodsUnit.label}')
                              ],
                            ),
                            Row(
                              children: [
                                const Text('采购重量：'),
                                Text('${g.totalWeight} 斤')
                              ],
                            ),
                            Row(
                              children: [
                                const Text('采购金额：'),
                                Text('¥ ${g.purchaseCost()}')
                              ],
                            ),
                            Row(
                              children: [
                                const Text('服务费用：'),
                                Text('¥ ${g.totalCost()}')
                              ],
                            ),
                          ],
                        )
                      : shrinkBox
                ],
              ),
            ),
            onTap: () {
              Logger.debug('tap ${g.goodsCode}');
              controller.goodsMap[g.goodsCode] = g.copyWith(expand: !g.expand);
            },
          ),
          const Divider(height: 0.0),
          InkWell(
            child: Container(
              padding: gapAll16,
              alignment: Alignment.center,
              child: g.expand
                  ? const Icon(Icons.keyboard_arrow_up)
                  : const Icon(Icons.keyboard_arrow_down),
            ),
            onTap: () {
              Logger.debug('arrow ${g.goodsCode}');
              controller.goodsMap[g.goodsCode] = g.copyWith(expand: !g.expand);
            },
          ),
        ],
      ),
    );
  }
}
